﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DatabaseHelper;

namespace UI.Online.SubForms
{
    public partial class frmPipelines : Form
    {
        public PProject CurrentProject;
        public int ProjectId;
        public frmPipelines()
        {
            InitializeComponent();
            btnRecords.Enabled = false;
            lvPipelineList.Columns.Add("Pipeline Name", 150, HorizontalAlignment.Left);
            lvPipelineList.Columns.Add("Summary", 150, HorizontalAlignment.Left);
        }

        public frmPipelines(PProject project)
            :this()
        {
            CurrentProject = project;
        }

        private void frmAddPipeline_Load(object sender, EventArgs e)
        {
            txtProjectName.Text = CurrentProject.ProjectName;
            CommonUseMethods cum = new CommonUseMethods();
            ProjectId = cum.FindProjectId(CurrentProject.ProjectName);
            cum.LoadPipelineList(ref lvPipelineList, ProjectId);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmNewPipeline frm = new frmNewPipeline(ProjectId, CurrentProject);
            frm.ShowDialog(); frm.Dispose();
            CommonUseMethods cum = new CommonUseMethods();
            cum.LoadPipelineList(ref lvPipelineList, ProjectId);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            DBHelper dh = new DBHelper();
            foreach (ListViewItem Item in lvPipelineList.SelectedItems)
            {
                string str = Item.SubItems[0].Text;
                dh.Query = "DELETE * FROM Pipelines WHERE PipelineName = '" + str + "'";
                dh.Execute();
                lvPipelineList.Items.Remove(Item);
            }
        }

        private void lvPipelineList_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRecords.Enabled = true;
        }

        private void btnRecords_Click(object sender, EventArgs e)
        {
            PPipeline pip = new PPipeline();
            pip.PipelineName = lvPipelineList.SelectedItems[0].SubItems[0].Text;
            pip.PipelineSummary = lvPipelineList.SelectedItems[0].SubItems[1].Text;
            frmRecords frm = new frmRecords(pip);
            frm.ShowDialog(); frm.Dispose();
        }
    }
}
